package com.zygame.sdk.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zygame.sdk.utils.res.ResourceUtil;

public class ImageEditText extends LinearLayout implements TextWatcher, View.OnClickListener {
	private Context context;
	private EditText fy_item_imageEditText_content;
	private TextView fy_item_imageEditText_icon;
	private ImageView fy_ie_history;
	private LinearLayout fy_item_imageEditText_root;
	private ImageView fy_item_imageEditText_line;
	private TextView fy_prompt_text;
	private ImageView fy_delete;

	private LinearLayout fy_item_imageEditText;

	private String promptStr;

	public ImageEditText(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	public ImageEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	@SuppressLint("NewApi")
	public ImageEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	/**
	 * @author K
	 * @Description: TODO(控件初始化)
	 * @date 2014-11-15 上午9:46:07
	 */
	private void init() {
		// TODO Auto-generated method stub
		LayoutInflater.from(context).inflate(ResourceUtil.getLayoutId(context, "zy_ly_item_imageedittext"), this, true);
		fy_item_imageEditText_content = (EditText) findViewById(ResourceUtil.getId(context, "fy_item_imageEditText_content"));
		fy_ie_history = (ImageView) findViewById(ResourceUtil.getId(context, "fy_ie_history"));
		fy_item_imageEditText_root = (LinearLayout) findViewById(ResourceUtil.getId(context, "fy_item_imageEditText_root"));
		fy_item_imageEditText_icon = (TextView) findViewById(ResourceUtil.getId(context, "fy_item_imageEditText_icon"));
		fy_item_imageEditText_line = (ImageView) findViewById(ResourceUtil.getId(context, "fy_item_imageEditText_line"));
		fy_prompt_text = (TextView) findViewById(ResourceUtil.getId(context, "fy_prompt_text"));
		fy_item_imageEditText = (LinearLayout) findViewById(ResourceUtil.getId(context, "fy_item_imageEditText"));
		fy_delete = (ImageView) findViewById(ResourceUtil.getId(context, "fy_delete"));
		fy_item_imageEditText_content.addTextChangedListener(this);
		fy_delete.setOnClickListener(this);
		setImageEditTextSize(14);
	}

	public <T> void setIcon(T t) {
		if (t instanceof Integer) {
			fy_item_imageEditText_icon.setBackgroundResource((Integer) t);
		} else if (t instanceof Drawable) {
			fy_item_imageEditText_icon.setBackgroundDrawable((Drawable) t);
		}
	}

	public void setHint(String str) {

		fy_item_imageEditText_content.setHint(str);
	}

	public void setHintid(String str) {
		fy_item_imageEditText_content.setHint(getResources().getText(ResourceUtil.getStringId(context, str)));
	}


	public void setImageEditTextSize(int size) {
		fy_item_imageEditText_content.setTextSize(size);
	}

	/**
	 * 设置输入框的
	 */
	public void setPwdInputType() {
		fy_item_imageEditText_content.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
	}

	public String getContent() {
		return fy_item_imageEditText_content.getText().toString().trim();
	}

	public <T> void setContent(T t) {
		if (t instanceof Integer) {
			fy_item_imageEditText_content.setText(getResources().getString((Integer) t));
		} else if (t instanceof String) {
			fy_item_imageEditText_content.setText((CharSequence) t);
		}
	}

	public void setContent(String str) {
		fy_item_imageEditText_content.setText(str);
	}

	/**
	 * 设置内容字体颜色
	 */
	public void setContentClole(String color) {
		fy_item_imageEditText_content.setTextColor(Color.parseColor(color));
	}

	public void setEditable(boolean flag) {
		fy_item_imageEditText_content.setEnabled(flag);
	}

	public ImageView getHistoryImageViwe() {
		if (fy_ie_history == null) {
			fy_ie_history = (ImageView) findViewById(ResourceUtil.getId(context, "fy_ie_history"));
		}
		return fy_ie_history;
	}

	public LinearLayout getRoot() {
		return fy_item_imageEditText_root;
	}

	public void hideHidstoryImageView() {
		if (fy_ie_history != null) {
			fy_ie_history.setVisibility(View.GONE);
		}
	}

	public void hideDelete() {
		if (fy_delete != null) {
			fy_delete.setVisibility(View.GONE);
		}
	}

	public void setText(String str) {
		correctImageSize();
		fy_item_imageEditText_icon.setBackgroundDrawable(null);
		fy_item_imageEditText_icon.setText(str);
	}

	/**
	 * 隐藏竖线
	 */
	public void setLineHide() {
		fy_item_imageEditText_line.setBackgroundDrawable(null);
	}


	/**
	 * 检测是否为空 0:账号 1:手机号 2:密码
	 */
	public boolean checkEmpty(int flag) {
		if (flag == 0) {
			fy_prompt_text.setVisibility(View.INVISIBLE);
		} else {
			fy_prompt_text.setVisibility(View.GONE);
		}
		if (TextUtils.isEmpty(getContent())) {
			fy_item_imageEditText.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_long_input_hover")));
			return true;
		}
		fy_item_imageEditText.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_long_input")));
		return false;
	}

	public void setErrorBg() {
		fy_item_imageEditText.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_long_input_hover")));
	}

	public void originalBG() {
		fy_item_imageEditText.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_long_input")));
	}

	public void setTextViewBG(String imgName) {
		fy_item_imageEditText.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, imgName)));
	}

	/**
	 * 设置提示框的内容
	 */
	public void setPromptText(String str) {
		fy_prompt_text.setBackgroundColor(000000);
		fy_prompt_text.setVisibility(View.VISIBLE);
		fy_prompt_text.setText(str);
		fy_prompt_text.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_inpuerror_red")));
	}
	public void setPromptTextId(String str) {
		fy_prompt_text.setBackgroundColor(000000);
		fy_prompt_text.setVisibility(View.VISIBLE);
		fy_prompt_text.setText(ResourceUtil.getStringId(context,str));
		fy_prompt_text.setBackgroundDrawable(getResources().getDrawable(ResourceUtil.getDrawableId(context, "fy_inpuerror_red")));
	}

	public void hidePromptText() {
		fy_prompt_text.setVisibility(View.INVISIBLE);
	}

	public void setString(String str) {
		this.promptStr = str;
	}

	/**
	 * 使用gone隐藏错误提示框
	 */
	public void hidePromptGone() {
		fy_prompt_text.setVisibility(View.GONE);
	}

	public void correctImageSize() {
		// ViewGroup.LayoutParams lp =
		// fy_item_imageEditText_icon.getLayoutParams();
		LayoutParams params = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		fy_item_imageEditText_icon.setLayoutParams(params);
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count, int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}

	@Override
	public void afterTextChanged(Editable s) {

		if (s.length() > 0) {
			fy_delete.setVisibility(VISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == ResourceUtil.getId(context,"fy_delete")){
			fy_item_imageEditText_content.setText("");
		}
	}

	interface HideTextListener{
		void hide();
	}
	private HideTextListener listener;
	public void setHideTextListener(HideTextListener listener){
		this.listener = listener;
	}
}
